package com.dj.mingkong.admin.biz.service.music.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.mingkong.admin.biz.controller.music.vo.request.AlbumAddVo;
import com.dj.mingkong.admin.biz.controller.music.vo.request.AlbumQueryVo;
import com.dj.mingkong.admin.biz.controller.music.vo.request.AlbumUpdateVo;
import com.dj.mingkong.admin.biz.controller.music.vo.response.AlbumVo;
import com.dj.mingkong.admin.biz.repository.database.music.*;
import com.dj.mingkong.admin.biz.service.music.AlbumService;
import com.dj.mingkong.common.enums.StatusEnum;
import com.dj.mingkong.common.exception.ValidateException;
import com.dj.mingkong.common.infrastructure.base.dto.delete.DeleteVo;
import com.dj.mingkong.common.infrastructure.eo.database.music.AlbumEo;
import com.dj.mingkong.common.util.AssertUtils;
import com.dj.mingkong.common.util.SmartBeanCopy;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 音乐专辑服务实现类
 *
 * @author zenglonghui
 * @date: 2022/7/21 16:04
 */
@Slf4j
@Service("albumService")
public class AlbumServiceImpl implements AlbumService {
    @Resource
    private AlbumRepository albumRepository;
    @Resource
    private AlbumCollectRepository albumCollectRepository;
    @Resource
    private AlbumLikeRepository albumLikeRepository;
    @Resource
    private AlbumMusicRepository albumMusicRepository;
    @Resource
    private AlbumVisitRepository albumVisitRepository;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(AlbumAddVo addVo) {
        checkAlbumName(addVo.getName());
        final AlbumEo albumEo = SmartBeanCopy.copy(addVo, AlbumEo.class);
        albumRepository.save(albumEo);
    }

    private void checkAlbumName(String name) {
        if (albumRepository.existByName(name)) {
            throw new ValidateException("The name already exists");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateById(AlbumUpdateVo updateVo) {
        final AlbumEo eo = albumRepository.getById(updateVo.getId());
        AssertUtils.isNotNull(eo, "Id invalid");
        if (!updateVo.getName().equals(eo.getName())) {
            checkAlbumName(updateVo.getName());
        }
        final AlbumEo albumEo = SmartBeanCopy.copy(updateVo, AlbumEo.class);
        albumRepository.updateById(albumEo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(DeleteVo deleteVo) {
        albumRepository.deleteById(deleteVo.getId());
        albumCollectRepository.deleteByAlbumId(deleteVo.getId());
        albumLikeRepository.deleteByAlbumId(deleteVo.getId());
        albumMusicRepository.deleteByAlbumId(deleteVo.getId());
        albumVisitRepository.deleteByAlbumId(deleteVo.getId());
    }

    @Override
    public Page<AlbumEo> pageByVo(AlbumQueryVo queryVo) {
        return albumRepository.listPageByVo(queryVo);
    }

    @Override
    public List<AlbumVo> listAll() {
        final List<AlbumEo> albumEos = albumRepository.list();
        final List<AlbumVo> albumVos = SmartBeanCopy.copy(albumEos, AlbumVo.class);
        if (CollectionUtils.isNotEmpty(albumVos)) {
            albumVos.forEach(vo -> vo.setDisabled(StatusEnum.INVALID.equalsIntVal(vo.getStatus())));
        }
        return albumVos;
    }

}
